package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.AddressMapper;
import com.wteam.dragon.shop.mapper.CouponHistoryMapper;
import com.wteam.dragon.shop.pojo.bean.Address;
import com.wteam.dragon.shop.service.AddressService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.exception.BadRequestException;
import com.wteam.dragon.system.util.UserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author 陈晓辉
 * @Date 2021/4/8 9:37
 */
@Service
@RequiredArgsConstructor
public class AddressServiceImpl implements AddressService {
    private final AddressMapper addressMapper;
    private final CouponHistoryMapper couponHistoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addAddress(Address address) {
        addressMapper.insert(address);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delAddress(Long id) {
        checkUser(id);
        addressMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editAddress(Address address) {
        checkUser(address.getId());
        addressMapper.updateById(address);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageAddress(Page<Address> page) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Address::getCreateId, UserUtil.getCurrentUserId())
                .orderByDesc(Address::getIsDefault);
        Page<Address> addressPage = addressMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("addressPage", addressPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getAddress(Long id) {
        checkUser(id);
        Address address = addressMapper.selectById(id);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("address", address);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage defaultAddress(Long id) {
        checkUser(id);
        //将该用户其他默认地址设为非默认地址
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Address::getIsDefault, true)
                .eq(Address::getCreateId, UserUtil.getCurrentUserId());
        Address one = addressMapper.selectOne(queryWrapper);
        if(one != null){
            one.setIsDefault(false);
            addressMapper.updateById(one);
        }
        //设为默认地址
        Address address = new Address();
        address.setId(id);
        address.setIsDefault(true);
        addressMapper.updateById(address);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("设置成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage cancelDefaultAddress(Long id) {
        checkUser(id);
        Address address = new Address();
        address.setId(id);
        address.setIsDefault(false);
        addressMapper.updateById(address);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("设置成功");
        return resultMessage;
    }

    @Override
    public ResultMessage getDefaultAddress() {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Address::getIsDefault, true)
                .eq(Address::getCreateId, UserUtil.getCurrentUserId());
        Address address = addressMapper.selectOne(queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("address", address);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    /**
     * 检测用户是否可以操作该地址信息
     */
    private void checkUser(Long addressId) {
        Address address = addressMapper.selectById(addressId);
        if (!address.getCreateId().equals(UserUtil.getCurrentUserId())) {
            throw new BadRequestException("您没有该权限");
        }
    }
}
